
const bookModel = require('../../models/books');
// const Sequelize = require('sequelize');
// const Op = Sequelize.Op; // 数据库查询规则

const indexControll = (router, root) => {
  /**
  * @api {get} /test?id=0 test
  * @apiDescription 测试get接口
  * @apiGroup Book
  * @apiParam {int} id 书id
  * @apiSuccess {int} code 成功: 0, 失败: 1
  * * @apiSuccess {string} msg 请求成功/失败
  * * @apiSuccess {json} data 返回内容
  * @apiSuccessExample {json} Success-Response:
  *  {
        code: 1,
        msg: '请求成功',
        data: []
  *  }
  * @apiSampleRequest http://localhost:3000/api/user/test
  * @apiVersion 1.0.0
  */
  router.get(root + '/test', async (ctx) => {

    const data = ctx.request.query;

    if (!data.id || data.id == '') {
      ctx.error('参数不能为空');
      return;
    }
    // await bookModel.insertData({
    //   title: '测试书籍',
    // })
    // .then(result => {
    //   console.log(result);
    // })
    // await bookModel.selectData({
    //   title: '测试书籍'
    // })
    // .then(result => {
    //   console.log(result);
    // })

    let selectResult = await bookModel.selectData({
      title: '无声告白'
    });

    // let result = await bookModel.updateData({
    //   title: '测试书籍'
    // }, {
    //   title: '测试书籍1'
    // });

    // await bookModel.deleteData({
    //   id: 32
    // })
    // .then(result => {
    //   console.log(result);
    // })
    if (selectResult) {
      ctx.success({});
    } else {
      // 还没想好是抛出还是直接返回
      ctx.error('操作失败');
      // ctx.throw('12345678');;
    }
  });

  /**
  * @api {post} /test_post test_post
  * @apiDescription 测试post接口
  * @apiGroup Book
  * @apiParam {string} title 书名
  * @apiSuccess {int} code 成功: 0, 失败: 1
  * * @apiSuccess {string} msg 请求成功/失败
  * * @apiSuccess {json} data 返回内容
  * @apiSuccessExample {json} Success-Response:
  *  {
        code: 0,
        msg: '请求成功',
        data: []
  *  }
  * @apiSampleRequest http://localhost:3000/api/user/test_post
  * @apiVersion 1.0.0
  */

  router.post(root + '/test_post', async (ctx) => {

    const book = ctx.request.body;
    let res = await bookModel.insertData({
      title: book.title,
    });

    if (res) {
      ctx.success({});
    } else {
      ctx.error('插入失败');
    }
  });
};

module.exports = indexControll;